//
//  MeiShiSViewController.m
//  MyProject
//
//  Created by apple on 15/11/9.
//  Copyright (c) 2015年 apple. All rights reserved.
//

#import "MeiShiSViewController.h"
#import "AFNetworking.h"
#import "MeiShiSModel.h"
#import "UIImageView+AFNetworking.h"
#import "LZXHelper.h"
#import "MeiShiDetailViewController.h"
#define URL @"http://www.youyuwei.com/api/country?list=city&"
@interface MeiShiSViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    NSMutableArray *_dataArr;
}

@property (nonatomic,strong)UITableView *tabelView;
@end

@implementation MeiShiSViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = self.namea;
    
    _dataArr = [[NSMutableArray alloc]init];
    
    [self getData];
    
    [self createView];
}

-(void)getData
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager GET:URL parameters:@{@"name":self.namea} success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil][@"data"];
        NSArray * arr = dic[@"list"];
        
        for (NSDictionary *subDic in arr) {
            MeiShiSModel *model = [[MeiShiSModel alloc]initWithDictionary:subDic error:nil];
            [_dataArr addObject:model];
        }
        
        [self.tabelView reloadData];
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);
    }];
}

-(void)createView
{
    self.tabelView = [[UITableView alloc]initWithFrame:self.view.bounds];
    [self.view addSubview:self.tabelView];
    self.tabelView.delegate = self;
    self.tabelView.dataSource = self;
    
    [self.tabelView registerNib:[UINib nibWithNibName:@"MeiShiSCell" bundle:nil] forCellReuseIdentifier:@"190"];
}

#pragma mark 代理方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MeiShiSModel *model = [_dataArr objectAtIndex:indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"190"];
    
    UIImageView *imgV = (UIImageView *)[cell.contentView viewWithTag:1000];
    [imgV setImageWithURL:[NSURL URLWithString:model.cover] placeholderImage:nil];
    UILabel *lable = (UILabel *)[cell.contentView viewWithTag:1001];
    lable.text = model.name;
    
    UILabel *lable1 = (UILabel *)[cell.contentView viewWithTag:1002];
    lable1.text = model.province;
    UILabel *lable2 = (UILabel *)[cell.contentView viewWithTag:1003];
    lable2.text = model.summary;
    
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MeiShiSModel *model = [_dataArr objectAtIndex:indexPath.row];
    float height = [LZXHelper textHeightFromTextString:model.summary width:[UIScreen mainScreen].bounds.size.width - 20 fontSize:15];
    
    return 230+height;
}


//点击了某个cell
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    MeiShiSModel *model = [_dataArr objectAtIndex:indexPath.row];
    MeiShiDetailViewController *meiShiDetail = [[MeiShiDetailViewController alloc]init];
    meiShiDetail.ID = model.id;
    meiShiDetail.titleName = model.name;
    [self.navigationController pushViewController:meiShiDetail animated:YES];
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
